package com.baizhi.controller;

import com.baizhi.entity.Department;
import com.baizhi.service.DepartmentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("department")
public class DepartmentController {
    private DepartmentService departmentService;
    @Autowired
    public void setDepartmentService(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @RequestMapping("showDepartment")
    public String showDepartment(String name, Integer pageNum, Integer pageSize, Map map){
        System.out.println("进入科室展示页面");
        if(pageNum==null) pageNum=1;
        if(pageSize==null) pageSize=2;
        PageInfo<Department> pageInfo = departmentService.selectByPage(name, pageNum, pageSize);
        map.put("list",pageInfo.getList());
        map.put("totalPage",pageInfo.getPages());
        map.put("pageNum",pageNum);
        map.put("name",name);
        return "manager/department/departmentList";
    }
    @RequestMapping("addDepartment")
    public String addDepartment(Department department){
        int i = departmentService.insert(department);
        return "redirect:showDepartment";
    }
    @RequestMapping("removeDepartment")
    public String removeDepartment(Integer did){
        int i = departmentService.delete(did);
        return "redirect:showDepartment";
    }
    @RequestMapping("removeAnyDepartment")
    public String removeAnyDepartment(int[] dids){
        if(dids.length>0){
            //复选框必须选中了才能删除  否则不删
            int i = departmentService.deleteAny(dids);
        }
        return "redirect:showDepartment";
    }
    @RequestMapping("getOneDepartment")
    public String getOneDepartment(Integer did,Map map){
        Department department = departmentService.selectById(did);
        map.put("department",department);
        System.out.println("修改前的参数"+department);
        return "manager/department/updateDepartment";
    }
    @RequestMapping("updateDepartment")
    public String updateDepartment(Department department){
        int i = departmentService.update(department);
        return "redirect:showDepartment";
    }
    @RequestMapping("selectGetJson")
    @ResponseBody
    public List<Department> selectGetJson(){
        List<Department> list = departmentService.selectGetJson();
        return list;
    }
    @RequestMapping("showTriageDepartment")
    public String showTriageDepartment(Map map){
        List<Department> departments = departmentService.selectTriageStatus();
        for (Department d : departments) {
            System.out.println("分诊首页的科室信息:"+d);
        }
        map.put("list",departments);
//        map.put("rownum",departments.size()/4);//每四个科室展示位一行  总共有多少行数
        return "manager/triage/triageDepartmentList";
    }
}
